package com.example.photography.fragment;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.photography.Activity.Menu_Activity;
import com.example.photography.Activity.Search_Activity;
import com.example.photography.Model;
import com.example.photography.R;
import com.example.photography.adapter.ViewStatePagerAdapter;
import com.example.photography.common.BaseFragment;
import com.example.photography.common.Constant;
import com.example.photography.fragment.home.Home_focus;
import com.example.photography.fragment.home.Home_Artaicle;
import com.example.photography.fragment.home.Home_course;
import com.example.photography.fragment.home.Home_recommend;
import com.example.photography.fragment.home.Home_scenery;
import com.example.photography.fragment.home.Home_vlog;
import com.google.android.material.tabs.TabLayout;
import com.loopj.android.http.RequestParams;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

import static android.app.Activity.RESULT_CANCELED;
import static androidx.constraintlayout.motion.widget.MotionScene.TAG;
import static com.example.photography.Activity.Menu_Activity.hr_change;

/**
 * 首页
 */
public class HomeFragment2 extends BaseFragment {
    @BindView(R.id.iv_seek)
    ImageView ivSeek;
    @BindView(R.id.tablayout_homeItemTab)
    TabLayout tablayoutHomeItemTab;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.vp_invest)
    ViewPager vpInvest;
    public static List<String> hrsigm_arr = new ArrayList<>();
    private ViewStatePagerAdapter viewStatePagerAdapter;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    protected void initData(String content) {
        String hr_sign = Model.getInstance().getDbManger().getUserInfoDao().getUserInfo(Constant.UID).getHr_sign();
        if (!hr_sign.equals("")){
            hrsigm_arr.addAll(Arrays.asList(hr_sign.split(",")));
        }

        initview();//初始化布局

        initListen();

    }

    private void initListen() {
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Menu_Activity.class);
                startActivityForResult(intent,1);
            }
        });

        ivSeek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Search_Activity.class);
                startActivity(intent);
            }
        });
    }

    private void initview() {

        //1. 加载多个不同的Fragment
        initFragments();
        //2.ViewPager设置多个Fragment的显示
//        MyAdapter myAdapter = new MyAdapter(getFragmentManager());
//        vpInvest.setAdapter(myAdapter);
        viewStatePagerAdapter = new ViewStatePagerAdapter(getActivity(),getChildFragmentManager(),fragmentList);
        vpInvest.setAdapter(viewStatePagerAdapter);
        vpInvest.setCurrentItem(1);//设置viewpager的初始化位置
        tablayoutHomeItemTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                在这里可以设置选中状态下  tab字体显示样式
                // TODO: 2020/11/11 大小和颜色修改失败
//                vpInvest.setCurrentItem(tab.getPosition());
//                View view = tab.getCustomView();
//                if (view instanceof TextView) {
//                    ((TextView) view).setTextSize(35);
////                    ((TextView) view).setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
//                }
                    Log.i(TAG, "onTabSelected:" + tab.getText());
                vpInvest.setCurrentItem(tab.getPosition());
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize_select);

//                TextView textView = new TextView(getActivity());
//                float selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16, getResources().getDisplayMetrics());
//                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,selectedSize);
//                textView.setTextColor(getResources().getColor(R.color.black));
//                textView.setText(tab.getText());
//                tab.setCustomView(textView);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //将TabLayout绑定ViewPager
        tablayoutHomeItemTab.setupWithViewPager(vpInvest);

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home2;
    }

    private List<Fragment> fragmentList = new ArrayList<>();

    private void initFragments() {
        //添加到集合中
        fragmentList.add(new Home_focus());
        fragmentList.add(new Home_recommend());
        fragmentList.add(new Home_Artaicle());
        fragmentList.add(new Home_vlog());
        addFragment();
    }
    private void addFragment(){//用户自定义添加的模块

        /**
         * hr_sign: ViewPager标识
         * 判断fragment是否加载
         */
        if (hrsigm_arr != null&&hrsigm_arr.size() > 0){
            for (String hr_sign : hrsigm_arr){
                switch (hr_sign){
                    case "人像":
                        fragmentList.add(new Home_course());
                        break;
                    case "风光":
                        fragmentList.add(new Home_scenery());
                        break;

                }
            }
        }

    }

    /**
     * 提供PagerAdapter的实现
     * 如果ViewPager中加载的是Fragment,则提供的Adapter可以继承于具体的：FragmentStatePagerAdapter或FragmentPagerAdapter
     * FragmentStatePagerAdapter:适用于ViewPager中加载的Fragment过多，会根据最近最少使用算法，实现内存中Fragment的清理，避免溢出
     * FragmentPagerAdapter:适合少量的页面显示。该类每一个生成的Fragment对象都会储存在内存中，所以适合相对静态、页面少的情况，如果是页面多，且Fragment的处理相对动态（添加、删除等）时，使用FragmentStatePagerAdapter较为适合。
     */
//    class MyAdapter extends FragmentPagerAdapter {
//
//        public MyAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public int getCount() {
//            return fragmentList == null ? 0 : fragmentList.size();
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            return fragmentList.get(position);
//        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            /**
//             * 因为一般数组一旦初始化成功就无法进行增删操作
//             * 所以我们要将其转List数组
//             */
//            List<String> home_tab = new ArrayList<String>(Arrays.asList(UIUtils.getStringArr(R.array.home_tab)));
//            if (hrsigm_arr!=null&&hrsigm_arr.length>0){
//                home_tab.addAll(Arrays.asList(hrsigm_arr));
//            }
//            //todo 放大字体显示不全
//            return home_tab.get(position);
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("requestCode", String.valueOf(requestCode));
        Log.e("resultCode", String.valueOf(resultCode));
        if (requestCode == 1) {
            if (resultCode == RESULT_CANCELED) {

                Log.e("hr_change", hr_change?"true":"false");
                if (hr_change){//fregment便签变化

                    if (hrsigm_arr != null&&hrsigm_arr.size()>0){//原来hr_sign不为空
                        //删除操作
                        for (int i = 0;i<hrsigm_arr.size();i++){
                            if (!Menu_Activity.listselect.contains(hrsigm_arr.get(i))){
                               fragmentList.remove(4+i);
                               hrsigm_arr.remove(i);
                            }
                        }
                        //增加操作
                        for (String temp : Menu_Activity.listselect){
                            if (!hrsigm_arr.contains(temp)){
                                hrsigm_arr.add(temp);
                                switch (temp){
                                    case "人像":
                                        fragmentList.add(new Home_course());
                                        break;
                                    case "风光":
                                        fragmentList.add(new Home_scenery());
                                        break;
                                }
                            }
                        }
                    }else {//原来hr_sign为空
                        //增加操作
                        for (String temp : Menu_Activity.listselect) {
                            if (!hrsigm_arr.contains(temp)) {
                                hrsigm_arr.add(temp);
                                switch (temp) {
                                    case "人像":
                                        fragmentList.add(new Home_course());
                                        break;
                                    case "风光":
                                        fragmentList.add(new Home_scenery());
                                        break;
                                }
                            }
                        }
                    }
                    viewStatePagerAdapter.notifyDataSetChanged();
                }
            }
        }

    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //隐藏时所作的事情
            GSYVideoManager.releaseAllVideos();
        } else {
            //显示时所作的事情
            GSYVideoManager.onResume();

        }
    }
}
